package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.TimeMarksDetileActivity;
import com.github.postsanf.yinian.bean.TimeMarks;
import com.github.postsanf.yinian.utils.UIHelper;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/9/25.
 */
public class TimeMarksAdapter extends BaseAdapter {

    private Context context;
    private List<TimeMarks.DataBean> mDatas;

    public TimeMarksAdapter(Context context, List<TimeMarks.DataBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        TimeMarks.DataBean dataBean = mDatas.get(position);
        return dataBean;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.time_marks_item, null);
            holder = new ViewHolder();
            holder.sd_head = ((SimpleDraweeView) convertView.findViewById(R.id.sd_head));
            holder.rl_bg = ((RelativeLayout) convertView.findViewById(R.id.rl_bg));
            holder.tv_times = ((TextView) convertView.findViewById(R.id.tv_times));
            holder.tv_title = ((TextView) convertView.findViewById(R.id.tv_title));
            holder.rl_bg_long = ((RelativeLayout) convertView.findViewById(R.id.rl_bg_long));
            holder.tv_times_long = ((TextView) convertView.findViewById(R.id.tv_times_long));
            holder.tv_title_long = ((TextView) convertView.findViewById(R.id.tv_title_long));
            holder.rl_rl_long = ((RelativeLayout) convertView.findViewById(R.id.rl_rl_long));
            convertView.setTag(holder);
        } else {
            holder = ((ViewHolder) convertView.getTag());
        }
        ImageOptHelper.showFrescoAvatar(Uri.parse(mDatas.get(position).getUpic()), holder.sd_head, context);

        final int days = getMiltime(mDatas.get(position).getMarkDate(), mDatas.get(position).getMarkRepeat());
        String day = String.valueOf(days);
        float percent = (float) (days * 1.0 / 364);
        int screenWidth = DisplayUtils.getScreenWidthPixels((Activity) context);
        int interSpace = DisplayUtils.dp2px(context, 75);
        int diffWidth = screenWidth - interSpace;
        if (days >= 364) {
            holder.rl_rl_long.setVisibility(View.VISIBLE);
            holder.rl_bg.setVisibility(View.GONE);
            if (mDatas.get(position).getMarkRepeat().equals("1")) {
                holder.tv_times_long.setText(day);
            } else {
                holder.tv_times_long.setText(day + "+");
            }
            holder.rl_bg_long.setBackgroundColor(Color.parseColor(mDatas.get(position).getMarkColor()));
            holder.tv_times_long.setSelected(true);
            holder.tv_title_long.setText(mDatas.get(position).getMarkContent());
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(diffWidth, UIHelper.dip2px(context, 40));
            holder.rl_bg_long.setLayoutParams(layoutParams);
            startAnimation(holder.rl_bg_long);
            holder.rl_bg_long.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TimeMarksDetileActivity.class);
                    intent.putExtra("timeMarks", (Serializable) mDatas);
                    intent.putExtra("position", position);
                    intent.putExtra("day", days);
                    context.startActivity(intent);
                }
            });
        } else if (days + 32 >= 182 && days < 364) {
            holder.rl_rl_long.setVisibility(View.VISIBLE);
            holder.rl_bg.setVisibility(View.GONE);
            if (mDatas.get(position).getMarkRepeat().equals("1")) {
                holder.tv_times_long.setText(day);
            } else {
                holder.tv_times_long.setText(day + "+");
            }
            holder.rl_bg_long.setBackgroundColor(Color.parseColor(mDatas.get(position).getMarkColor()));
            holder.tv_times_long.setSelected(true);
            holder.tv_title_long.setText(mDatas.get(position).getMarkContent());
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (percent * diffWidth), UIHelper.dip2px(context, 40));
            holder.rl_bg_long.setLayoutParams(layoutParams);
            startAnimation(holder.rl_bg_long);
            holder.rl_bg_long.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TimeMarksDetileActivity.class);
                    intent.putExtra("timeMarks", (Serializable) mDatas);
                    intent.putExtra("position", position);
                    intent.putExtra("day", days);
                    context.startActivity(intent);
                }
            });
        } else {
            holder.rl_rl_long.setVisibility(View.GONE);
            holder.rl_bg.setVisibility(View.VISIBLE);
            holder.tv_times.setBackgroundColor(Color.parseColor(mDatas.get(position).getMarkColor()));
            holder.tv_times.setSelected(true);
            holder.tv_title.setText(mDatas.get(position).getMarkContent());
            if (mDatas.get(position).getMarkRepeat().equals("1")) {
                holder.tv_times.setText(day);
            } else {
                holder.tv_times.setText(day + "+");
            }
            RelativeLayout.LayoutParams layoutParams = null;

            if (mDatas.get(position).getMarkRepeat().equals("0")) {
                if (days < 55) {
                    layoutParams = new RelativeLayout.LayoutParams(UIHelper.dip2px(context, 42), UIHelper.dip2px(context, 40));
                } else {
                    layoutParams = new RelativeLayout.LayoutParams((int) (percent * diffWidth), UIHelper.dip2px(context, 40));
                }
            } else {
                if (days < 40) {
                    layoutParams = new RelativeLayout.LayoutParams(UIHelper.dip2px(context, 32), UIHelper.dip2px(context, 40));
                } else {
                    layoutParams = new RelativeLayout.LayoutParams((int) (percent * diffWidth), UIHelper.dip2px(context, 40));
                }
            }

            holder.tv_times.setLayoutParams(layoutParams);
            startAnimation(holder.tv_times);
            holder.rl_bg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, TimeMarksDetileActivity.class);
                    intent.putExtra("timeMarks", (Serializable) mDatas);
                    intent.putExtra("position", position);
                    intent.putExtra("day", days);
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    private void startAnimation(View v) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.2f, 1.0f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);
        scaleAnimation.setDuration(500);
        scaleAnimation.setFillAfter(true);
        v.setAnimation(scaleAnimation);
        scaleAnimation.startNow();
    }

    private int getMiltime(String time, String repeat) {
        //获取当前时间的毫秒值并根据重复判断得出天数
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long timeMil = 0;
        try {
            Date parse = simpleDateFormat.parse(time);
            timeMil = parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long currentTime = getCurrentTime();
        long diffMills = currentTime - timeMil;
        int days;
        if (repeat.equals("1") && diffMills > 0) {
//            int i = (int) (diffMills / 1000 / 3600 / 24) / 365;
//            int n = (int) (diffMills / 1000 / 3600 / 24) - 365 * i;
//            if (i>0){
//                days = n * (-1) + 365+1;
//            }else{
//                days = n * (-1) + 365;
//            }
            long currentMillis = System.currentTimeMillis();
            Date curDate = new Date(currentMillis);
            String time1 = simpleDateFormat.format(curDate);

            String[] split = time1.split("-");
            int yearInt = Integer.parseInt(split[0])+1;
            String[] split1 = time.split("-");
            time=String.valueOf(yearInt)+"-"+split1[1]+"-"+split1[2];
//            Log.d("yuhao", "time="+time);
            try {
                Date parse = simpleDateFormat.parse(time);
                timeMil = parse.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            diffMills = currentTime - timeMil;
        }
        diffMills = Math.abs(diffMills);
        days = (int) (diffMills / 1000 / 3600 / 24);
        return days;
    }

    private Long getCurrentTime() {
        long currentMillis = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(currentMillis);
        String time = formatter.format(curDate);

        long timeMil = 0;
        try {
            Date parse = formatter.parse(time);
            timeMil = parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeMil;
    }

    public class ViewHolder {
        TextView tv_times;
        TextView tv_title;
        SimpleDraweeView sd_head;
        RelativeLayout rl_bg;

        TextView tv_times_long;
        TextView tv_title_long;
        SimpleDraweeView sd_head_long;
        RelativeLayout rl_bg_long;
        RelativeLayout rl_rl_long;
    }
}
